﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using produccion.clases;
using System.Data;
using System.IO;

namespace produccion.forms
{
    /// <summary>
    /// Lógica de interacción para frmCamDesRef.xaml
    /// </summary>
    public partial class frmCamDesRef : Window
    {
        CLSENCPPCP EncPpcp = new CLSENCPPCP();
        CLSDETPPCP DetPpcp = new CLSDETPPCP();
        DataTable dt = new DataTable();

        public frmCamDesRef()
        {
            InitializeComponent();
        }

        private void btnSalir_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {

        }

        private void btnBuscarAct_Click(object sender, RoutedEventArgs e)
        {
            FRMBuscarCol frm = new FRMBuscarCol();
            frm.EsReferencia = true;
            frm.RequiereColor = true;
            frm.RequiereTalla = true;
            //frm.btnAceptar.Visibility = Visibility.Hidden;
            frm.ShowDialog();            
            txtRefAct.Text = frm.Retorno;
            if (!txtRefAct.Text.Equals(""))
            {
                txtNomAct.Text = EncPpcp.NombreRef(txtRefAct.Text.ToString().Substring(0,4));
                buscar();
            }
        }

        CLSMAEREF MaeRef = new CLSMAEREF();

        private void btnBuscarNue_Click(object sender, RoutedEventArgs e)
        {
            FRMBuscarCol frm = new FRMBuscarCol();
            frm.EsReferencia = true ;
            frm.RequiereColor = true;
            frm.RequiereTalla = true;
            //frm.btnAceptar.Visibility = Visibility.Hidden;
            frm.ShowDialog();
            txtRefNue.Text = frm.Retorno;
            if (!txtRefNue.Text.Equals(""))
            {
                txtNomNue.Text = EncPpcp.NombreRef(txtRefNue.Text.ToString().Substring(0,4));
            }
            if (MaeRef.TallajeRef(txtRefAct.Text) != MaeRef.TallajeRef(txtRefNue.Text))
            {
                MessageBox.Show("El tallaje de las referencias deben ser iguales", "Estándares", MessageBoxButton.OK, MessageBoxImage.Exclamation);
                txtRefNue.Text = "";
                return;
            }
        }

        private void txtRefAct_TextChanged(object sender, TextChangedEventArgs e)
        {
            txtNomAct.Text = "";
            imgFotoAct.Source = null;
        }

        private void txtRefNue_TextChanged(object sender, TextChangedEventArgs e)
        {
            txtNomNue.Text = "";
            imgFotoNue.Source = null;
        }

        private void txtRefAct_LostFocus(object sender, RoutedEventArgs e)
        {
            buscar();
        }

        private void buscar()
        {
            try
            {
                if (!txtRefAct.Text.Equals("") && txtRefAct.Text.Length == 6)
                {
                    txtNomAct.Text = EncPpcp.NombreRef(txtRefAct.Text.ToString().Substring(0, 4));
                    DetPpcp.CODREF_DPC = txtRefAct.Text.ToString().Substring(0, 4);
                    DetPpcp.CODCOL_DPC = txtRefAct.Text.ToString().Substring(4, 2);
                    dt = DetPpcp.PedidosCambiarDescartar();
                    dt.Columns.Add(new DataColumn("OK", System.Type.GetType("System.Boolean")));
                    dt.Columns["OK"].DefaultValue = false;
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        dt.Rows[i]["OK"] = false;
                    }
                    dgPedidos.ItemsSource = dt.DefaultView;
                    dgPedidos.CanUserAddRows = false;
                    dgPedidos.CanUserDeleteRows = false;
                    BitmapImage b = new BitmapImage();
                    if (File.Exists(MDOgeneral.Fotos_BOSI + DetPpcp.CODREF_DPC + DetPpcp.CODCOL_DPC + ".jpg"))
                    {
                        b.BeginInit();
                        b.UriSource = new Uri(MDOgeneral.Fotos_BOSI + DetPpcp.CODREF_DPC + DetPpcp.CODCOL_DPC + ".jpg");
                        b.EndInit();
                        imgFotoAct.Stretch = Stretch.Fill;
                        imgFotoAct.Source = b;
                    }
                    else
                    {
                        imgFotoAct.Source = null;
                    }
                }
                if (txtNomAct.Text == "")
                {
                    dgPedidos.ItemsSource = null;
                    txtRefAct.Text = "";
                }
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); }
        }

        private void txtRefNue_LostFocus(object sender, RoutedEventArgs e)
        {
            if (txtRefNue.Text == txtRefAct.Text)
            {
                MessageBox.Show("Las referencias no pueden ser iguales!", "Cambiar / Descartar pedidos de materia prima", MessageBoxButton.OK, MessageBoxImage.Error);
                txtRefNue.Text = "";
                return;
            }
            if (!txtRefNue.Text.Equals("") && txtRefNue.Text.Length == 6)
            {
                txtNomNue.Text = EncPpcp.NombreRef(txtRefNue.Text.ToString().Substring(0, 4));
                if (MaeRef.TallajeRef(txtRefAct.Text) != MaeRef.TallajeRef(txtRefNue.Text))
                {
                    MessageBox.Show("El tallaje de las referencias deben ser iguales", "Estándares", MessageBoxButton.OK, MessageBoxImage.Exclamation);
                    txtRefNue.Text = "";
                    return;
                }
                BitmapImage b = new BitmapImage();
                if (File.Exists(MDOgeneral.Fotos_BOSI + txtRefNue.Text.Substring(0, 6) + ".jpg"))
                {
                    b.BeginInit();
                    b.UriSource = new Uri(MDOgeneral.Fotos_BOSI + txtRefNue.Text.Substring(0, 6) + ".jpg");
                    b.EndInit();
                    imgFotoNue.Stretch = Stretch.Fill;
                    imgFotoNue.Source = b;
                }
                else
                {
                    imgFotoNue.Source = null;
                }
                if (txtNomNue.Text == "")
                {
                    txtRefNue.Text = "";
                }
            }
        }

        private void dgPedidos_GotFocus(object sender, RoutedEventArgs e)
        {
            int j = dgPedidos.CurrentCell.Column.DisplayIndex;
            if (j == 5)
            {
                dgPedidos.IsReadOnly = false;
            }
            else
            {
                dgPedidos.IsReadOnly = true;
            }
        }

        private void rbCambiar_Checked(object sender, RoutedEventArgs e)
        {
            if (this.IsLoaded == true)
            {
                border4.Visibility = Visibility.Visible;
            }
        }

        private void rbDescartar_Checked(object sender, RoutedEventArgs e)
        {
            border4.Visibility = Visibility.Hidden;
        }

        private void btnNuevo_Click(object sender, RoutedEventArgs e)
        {
            txtRefAct.Text = "";
            txtRefNue.Text = "";
            txtNomAct.Text = "";
            txtNomNue.Text = "";
            dgPedidos.ItemsSource = null;
            dgPedidos.Items.Clear();
            dt.Clear();
            imgFotoAct.Source = null;
            imgFotoNue.Source = null;
        }

        private void btnGrabar_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (txtRefAct.Text == txtRefNue.Text)
                {
                    MessageBox.Show("Las referencias no pueden ser iguales!", "Cambiar / Descartar pedidos de referencias", MessageBoxButton.OK, MessageBoxImage.Error);
                    return;
                }
                if (rbCambiar.IsChecked == true)
                {
                }
                if (rbCambiar.IsChecked == true)
                {
                    if (!txtRefAct.Text.Equals("") && !txtRefNue.Text.Equals(""))
                    {
                        for (int i = 0; i < dt.Rows.Count; i++)
                        {
                            if (dt.Rows[i][5].Equals(true))
                            {
                                DetPpcp.CODREF_DPC = txtRefAct.Text.Substring(0, 4);
                                DetPpcp.CODCOL_DPC = txtRefAct.Text.Substring(4, 2);
                                DetPpcp.CODCLI_DPC = dt.Rows[i][1].ToString();
                                DetPpcp.NROPED_DPC = System.Convert.ToInt32(dt.Rows[i][0].ToString());
                                DetPpcp.DescartarInsertar(txtRefNue.Text.Substring(0, 4), txtRefNue.Text.Substring(4, 2));
                                if (txtDetalles.Text != "")
                                {
                                    EncPpcp.CODCLI_EPC = DetPpcp.CODCLI_DPC;
                                    EncPpcp.NROPED_EPC = DetPpcp.NROPED_DPC;
                                    EncPpcp.OBSPED_EPC = EncPpcp.OBSPED_EPC + txtDetalles.Text;
                                    EncPpcp.ActualizarObservaciones();
                                }
                            }
                        }
                    }
                }
                else
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        if (!txtRefAct.Text.Equals(""))
                        {
                            if (dt.Rows[i][5].Equals(true))
                            {
                                DetPpcp.CODREF_DPC = txtRefAct.Text.Substring(0, 4);
                                DetPpcp.CODCOL_DPC = txtRefAct.Text.Substring(4, 2);
                                DetPpcp.CODCLI_DPC = dt.Rows[i][1].ToString();
                                DetPpcp.NROPED_DPC = System.Convert.ToInt32(dt.Rows[i][0].ToString());
                                DetPpcp.DescartarTodo();
                                if (txtDetalles.Text != "")
                                {
                                    EncPpcp.CODCLI_EPC = DetPpcp.CODCLI_DPC;
                                    EncPpcp.NROPED_EPC = DetPpcp.NROPED_DPC;
                                    EncPpcp.OBSPED_EPC = EncPpcp.OBSPED_EPC + txtDetalles.Text;
                                    EncPpcp.ActualizarObservaciones();
                                }
                            }
                        }
                    }
                }
                txtRefAct.Text = "";
                txtRefNue.Text = "";
                txtNomAct.Text = "";
                txtNomNue.Text = "";
                txtDetalles.Text = "";
                dgPedidos.ItemsSource = null;
                dgPedidos.Items.Clear();
                dt.Clear();
                imgFotoAct.Source = null;
                imgFotoNue.Source = null;

                new MDOgeneral.notificar(wPnot, "Los cambios fueron guardados correctamente.", true);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error al grabar cambia - descartar ref: " + ex.Message, "Cambiar / Descartar", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        private void Window_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                //System.Windows.Forms.SendKeys.Send("{TAB}");
                KeyEventArgs e1 = new KeyEventArgs(Keyboard.PrimaryDevice, Keyboard.PrimaryDevice.ActiveSource,
                                 0, Key.Tab);
                e1.RoutedEvent = Keyboard.KeyDownEvent;
                InputManager.Current.ProcessInput(e1);
            }
        }

        private void Window_Loaded_1(object sender, RoutedEventArgs e)
        {
            Keyboard.Focus(txtRefAct);
        }

        private void border5_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            var elemento = e.OriginalSource as UIElement;
            if (e.Key == Key.Enter && elemento != null)
            {
                e.Handled = true;
                elemento.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
            }
        }

        private void Window_KeyDown_1(object sender, KeyEventArgs e)
        {
            var elemento = e.OriginalSource as UIElement;
            if (e.Key == Key.F3 && elemento != null)
            {
                if (MDOgeneral.logBusquedadAbierta == true) { MDOgeneral.logBusquedadAbierta = false; }
            }

            if (e.Key == Key.F4 && elemento != null)
            {
                if (MDOgeneral.logBusquedadAbierta1 == true) { MDOgeneral.logBusquedadAbierta1 = false; }
            }

            if (e.Key == Key.F5 && elemento != null)
            {
                if (MDOgeneral.logBusquedadAbierta2 == true) { MDOgeneral.logBusquedadAbierta2 = false; }
            }
        }

    }
}
